#include    "battery.h"

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Battery::Battery(QObject *parent) : Device(parent)
  , r(1.0)
  , Rd(1.0)
  , In(0.0)
  , Is(0.0)
  , Ib(0.0)
  , Emax(96.0)
  , Emin(84.0)
  , C(450.0)
  , U_gen(0.0)
{

}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Battery::~Battery()
{

}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
double Battery::getVoltage() const
{
    return pf(getY(0) - r * In);
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
double Battery::getCargeCurrent() const
{
    return Ib;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::setLoadCurrent(double In)
{
    this->In = In;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::setStarterCurrent(double Is)
{
    this->Is = Is;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::setChargeVoltage(double U_gen)
{
    this->U_gen = U_gen;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::preStep(state_vector_t &Y, double t)
{
    Q_UNUSED(t)

    Y[0] = cut(Y[0], 0.0, Emax);

    if (U_gen <= Y[0])
    {
        Ib = -In;
    }
    else
    {
        Ib = (U_gen - Y[0]) / (r + Rd);
    }
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::ode_system(const state_vector_t &Y,
                         state_vector_t &dYdt,
                         double t)
{
    Q_UNUSED(Y)
    Q_UNUSED(t)

    dYdt[0] = (Ib - Is) * (Emax - Emin) / C / 3600;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Battery::load_config(CfgReader &cfg)
{
    QString secName = "Device";

    cfg.getDouble(secName, "Emax", Emax);

    setY(0, Emax);

    cfg.getDouble(secName, "Emin", Emin);
    cfg.getDouble(secName, "r", r);
    cfg.getDouble(secName, "Rd", Rd);
    cfg.getDouble(secName, "Capacity", C);
}
